package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcEquipment;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentChildrenType;

/**
 * @ClassName: DaoDcEquipmentChildrenType 
 * @Description:数据中心_项目_项目设备子类型维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcEquipmentChildrenType extends EntityRepositoryServiceImpl<DcEquipmentChildrenType>{

	public DaoDcEquipmentChildrenType(Dao dao) {
		super(dao);
	}
	
	public QueryResult findEquipmentChildrenType(NutMap nutMap){
		List <Map> page_list = null;
		Pager pager = null;
		StringBuffer stringBuffer = new StringBuffer("select dect.*, det.equipment_type_name "
				+ " from dc_equipment_children_type dect "
				+ " left join dc_equipment_type det on dect.equipment_type_id = det.id and det.del_flag = '0' "
				+ " where dect.del_flag = '0' ");
		if(nutMap.get("equipment_type_id") != null){
			stringBuffer.append(" and det.id = @equipment_type_id ");
		}
		if(nutMap.get("type_name") != null){
			stringBuffer.append(" and dect.equipment_children_type_name like @type_name ");
		}
		stringBuffer.append(" order by dect.create_time desc ");
		Sql sql = Sqls.create(stringBuffer.toString());
		if(nutMap.get("equipment_type_id") != null){
			sql.params().set("equipment_type_id", nutMap.get("equipment_type_id"));
		}
		if(nutMap.get("type_name") != null){
			sql.params().set("type_name", "%"+nutMap.get("type_name")+"%");
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"), nutMap.getInt("page_size"));
			pager.setRecordCount(findEquipmentTypeNum(nutMap));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			List <Map> list = new ArrayList<>();
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					Map map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("equipment_children_type_code", rs.getString("equipment_children_type_code"));
					map.put("equipment_children_type_name", rs.getString("equipment_children_type_name"));
					map.put("equipment_type_id", rs.getString("equipment_type_id"));
					map.put("equipment_type_name", rs.getString("equipment_type_name"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					list.add(map);
				}
				SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(Map.class);
		return new QueryResult(page_list, pager);
	}
	
	public int findEquipmentTypeNum(NutMap nutMap){
		StringBuffer stringBuffer = new StringBuffer("select count(dect.id) num "
				+ " from dc_equipment_children_type dect "
				+ " left join dc_equipment_type det on dect.equipment_type_id = det.id and det.del_flag = '0' "
				+ " where dect.del_flag = '0' ");
		if(nutMap.get("equipment_type_id") != null){
			stringBuffer.append(" and det.id = @equipment_type_id ");
		}
		Sql sql = Sqls.create(stringBuffer.toString());
		if(nutMap.get("equipment_type_id") != null){
			sql.params().set("equipment_type_id", nutMap.get("equipment_type_id"));
		}
		sql.setCallback(new SqlCallback() {
			int num = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}

}
